from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles

from handler import http_exception_handler
from route import user_router
from middleware import JwtMiddleware
from db import init_db
from contextlib import asynccontextmanager


@asynccontextmanager
async def app_lifespan(app: FastAPI):
    print("fastapi project startup")
    await init_db(app)
    yield

    print("fastapi project shutdown")


app = FastAPI(lifespan=app_lifespan)

# 挂载静态文件目录
app.mount("/static", StaticFiles(directory="static"), name="static")

app.add_exception_handler(HTTPException, http_exception_handler)
app.add_middleware(JwtMiddleware)
app.include_router(user_router)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app="main:app", host="0.0.0.0", port=8000, reload=True)
